﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WGM_Client_Net35V2.HelperClasses;
using WGM_Client_Net35V2.Controls;
using System.Windows.Media.Effects;
using WGM_Client_Net35V2.Properties;

namespace WGM_Client_Net35V2.Views
{
    /// <summary>
    /// Interaction logic for PlacesView.xaml
    /// </summary>
    public partial class PlacesView : UserControl
    {
        public PlacesView()
        {
            ClassRegistry.AddClass(this);
            InitializeComponent();
        }
        public void AddPlace(WGM_Svc.WGMNetwork placeData)
        {
            Place existing = FindPlace(placeData.NetworkName);
            if(existing != null)
            {
                existing.SetPlaceData(placeData);
                return;
            }
            Place current = GetCurrentPlace();
            

            Place place = new Place(placeData);
            if (current != null)
            {
                place.SetEnabled(false);
                place.SetCurrentPlace(false);
            }

            for (int count = 0; count < _placesPanel.Children.Count; count++)
            {
                if (place.PlaceName.ToLower().CompareTo((_placesPanel.Children[count] as Place).PlaceName.ToLower()) < 0)
                {
                    _placesPanel.Children.Insert(count, place);
                    return;
                }
            }
            _placesPanel.Children.Add(place);
        }
        public void RemovePlace(Place place)
        {
            _placesPanel.Children.Remove(place);
        }
        public Place FindPlace(string placeName)
        {
            foreach (var place in _placesPanel.Children.Cast<Place>())
            {
                if (place.PlaceName == placeName)
                    return place;
            }
            return null;
        }
        public Place GetCurrentPlace()
        {
            foreach (var place in _placesPanel.Children.Cast<Place>())
            {
                if (place.IsCurrentPlace)
                    return place;
            }
            return null;
        }
        public void ClearPlaces()
        {
            _placesPanel.Children.Clear();
        }
        private void OnRefreshPlacesClick(object sender, MouseButtonEventArgs e)
        {
            (ClassRegistry.GetClass(typeof(MainWindow)) as MainWindow).RetrievePlaces();
        }
        public void SetCurrentPlace(Place currPlace)
        {
            Settings.Default.LastNetwork = currPlace.PlaceName;
            foreach (var place in _placesPanel.Children.Cast<Place>())
            {
                place.SetEnabled(false);
                if (place != currPlace)
                {
                    place.SetCurrentPlace(false);                    
                }
                else
                {
                    place.SetCurrentPlace(true);
                }
            }
            _leaveCurrentPlaceBtn.IsEnabled = true;
            _leaveCurrentPlaceBtn.Foreground = new SolidColorBrush(Colors.DarkSlateGray);
            
        }
        public void ClearCurrentPlace()
        {
            _leaveCurrentPlaceBtn.IsEnabled = false;
            _leaveCurrentPlaceBtn.Foreground = new SolidColorBrush(Colors.LightGray);
            Settings.Default.LastNetwork = null;
            foreach (var place in _placesPanel.Children.Cast<Place>())
            {
                place.SetEnabled(true);
                place.SetCurrentPlace(false);
            }
        }
    }
}
